package frontend;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;

import domain.Library;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

public class MasterJFrame extends JFrame implements Observer {
    private static final long serialVersionUID = -1761899999291798063L;
    private JPanel contentPane;
    private BookMasterJPanel booksJPanel;
    private LoanMasterJPanel loansJPanel;

    /**
     * Create the frame.
     */
    public MasterJFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 750, 550);
        setMinimumSize(new Dimension(750, 550));
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        contentPane.add(tabbedPane, BorderLayout.CENTER);

        booksJPanel = new BookMasterJPanel();
        loansJPanel = new LoanMasterJPanel();
        tabbedPane.addTab("Bücher", null, booksJPanel, null);
        tabbedPane.addTab("Ausleihen", null, loansJPanel, null);

        Library.getInstance().addObserver(this);
        attachActionListeners();
    }

    private void attachActionListeners() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                onClose();
            }
        });
    }

    private void onClose() {
        Library.getInstance().deleteObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        booksJPanel.updateFromLibrary();
        loansJPanel.updateFromLibrary();
    }
}
